<template>
	<view>
		<view class="bg">
			<image class="image" :src="img"></image>
			<view class="bg_title">
				<view>总贡献</view>
				<view>{{handlePrice(total_contribute)}}</view>
			</view>
		</view>

		<view class="ranking">
			<view class="ranking_title">
				<view>头像昵称</view>
				<view>预计补贴</view>
			</view>

			<view class="ranking_item" v-for="(item,index) in dataList" :key="index">
				<view class="item_left">
					<!-- <view class="icon">
						<image class="image" :src="item.rank_img" v-if="item.rank_img.length"></image>
						<view class="rank" v-else>{{index+1<10?'0'+(index+1):index+1}}</view>
					</view> -->
					<view class="avatar_info">
						<view class="avatar" v-if="item.avatar">
							<image class="image" :src="item.avatar"></image>
						</view>
						<view class="avatar" v-else>
							<image class="image" src="../../static/image/user-default-avatar.png" ></image>
						</view>
						<view class="info">
							<view class="user_name u-line-1">{{item.nickname}}</view>
							<!-- <view class="phone">{{item.phone}}</view> -->
						</view>
					</view>
				</view>

				<view class="number_info">
					<!-- {{item.percent != '0%' ? (item.percent) : ''}} -->
					<view class="integral">贡献值:{{item.contribute}}</view>
					<view class="estimate">预计补贴:￥{{item.bonus}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				img: 'https://yfl.yflbp.com/bg.png',
				dataList: [],
				all_price: 0,
				page: 1,
				total_contribute:0,
			}
		},
		methods: {
			//下拉刷新：
			onPullDownRefresh() {
			 this.dataList = []
			 this.all_price = 0
			 this.page = 1,
			 this.getRanking()
			},
			//上拉加载：
			onReachBottom() {
			 this.page++
			 this.getRanking();
			},
			getRanking() {
				this.$request({
						url: this.$api.index.ranking,
						method: 'get',
						data: {
						    limit: 10,
							page: this.page,
						}
					})
					.then(res => {
						console.log(res);
						// this.img = res.img;
						this.total_contribute = res.total_contribute;
						res.data.forEach((item)=>{
							this.dataList.push(item);
						})
						
						this.all_price = res.total_bonus;
						uni.stopPullDownRefresh()
					}).catch(err => {
						console.log(err);
					});
			},
			/**
			 * 处理金额
			 * @param price{Number|String} 金额
			 * @return {String}
			 */
			handlePrice(price) {
				console.log(price);
				let w = 10000; // 万
				let y = 100000000; // 亿
				if (typeof(price) === 'number' || !isNaN(parseInt(price))) {
					if (price >= y) {
						if (price % y < y / 100) {
							return (price / y).toFixed(0) + '亿'
						} else {
							return (price / y).toFixed(2) + '亿'
						}
					} else if (price >= w) {
						if (price % w < w / 100) {
							return (price / w).toFixed(0) + '万'
						} else {
							return (price / w).toFixed(2) + '万'
						}
					} else {
						return price + ''
					}
				} else {
					throw new Error('金额不是一个Number|String类型')
				}
			}
		},
		onLoad() {
			this.getRanking()
			console.log(this.handlePrice(100));
		},
		
	}
</script>

<style scoped lang="scss">
	.image {
		width: 100%;
		height: 100%;
	}

	.bg {
		position: relative;
		width: 750rpx;
		height: 380rpx;

		.bg_title {
			position: absolute;
			top: 20%;
			left: 50%;
			transform: translateX(-50%);
			font-size: 56rpx;
			color: #D66411;
			font-weight: 800;
			text-align: center;

			:first-child {
				margin-bottom: 38rpx;
			}
		}
	}

	.ranking {
		padding: 60rpx 32rpx;
		transform: translateY(-20rpx);
		width: 750rpx;
		background: #FFFFFF;
		border-radius: 24rpx 24rpx 0rpx 0rpx;

		.ranking_title {
			display: flex;
			justify-content: space-between;
			color: #666;
			font-size: 32rpx;
		}

		.ranking_item {
			display: flex;
			justify-content: space-between;
			padding: 32rpx 0;
			font-size: 28rpx;
			border-bottom: 1px solid #F5F5F5;

			.item_left {
				display: flex;
				flex: 1;

				.icon {
					width: 80rpx;
					height: 80rpx;
					margin-right: 40rpx;
					text-align: center;

					.rank {
						font-size: 36rpx;
						color: #999999;
						line-height: 80rpx;
						font-weight: 800;
					}
				}

				.avatar_info {
					display: flex;

					.avatar {
						width: 80rpx;
						height: 80rpx;
						border-radius: 50%;
						overflow: hidden;
						margin-right: 16rpx;
					}

					.info {
						display: flex;
						flex-direction: column;

						.user_name {
							max-width: 200rpx;
						}

						.phone {
							color: #888;
							font-size: 24rpx;
						}
					}
				}
			}

			.number_info {
				flex: 1;
				text-align: right;

				.estimate {
					color: #888;
					font-size: 24rpx;
				}
			}
		}
	}
</style>
